//
//  WBOAuthController.m
//  sinaWeibo
//
//  Created by 李江波 on 2016/11/15.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "WBOAuthController.h"
#import <SVProgressHUD.h>
#import "LEEHTTPManager.h"
#import "WBUserInfoModel.h"
#import "WBOAuthViewModel.h"
#import "WBWelcomeController.h"

@interface WBOAuthController ()<UIWebViewDelegate>
    
@property(nonatomic ,strong) UIWebView *webView;

    
@end

@implementation WBOAuthController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(closeOAuth)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"自动填充" style:UIBarButtonItemStylePlain target:self action:@selector(autoUser)];
    
    _webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    
    _webView.delegate = self;
    
    [self.view addSubview:_webView];
    
    NSString *urlStr = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",client_id,redirect_uri];
    
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
    
}


/**
 关闭页面
 */
-(void)closeOAuth {

    [self dismissViewControllerAnimated:YES completion:nil];

}


/**
 自动填充
 */
-(void)autoUser {

    NSString *jsStr = @"document.getElementById('userId').value = '15712691251', document.getElementById('passwd').value = 'li789456123'";
    
    [_webView stringByEvaluatingJavaScriptFromString:jsStr];
}

-(void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
    
    [SVProgressHUD dismiss];

}
    
#pragma mark : - uiwebview代理

-(void)webViewDidStartLoad:(UIWebView *)webView{

    [SVProgressHUD show];
    
}
    
-(void)webViewDidFinishLoad:(UIWebView *)webView{

    [SVProgressHUD dismiss];

}

    //拿到code
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    NSString *urlStr = request.URL.absoluteString;
    NSString *code = @"code=";
    if ([urlStr containsString:code]) {
        
        NSString *query = request.URL.query;
        
        NSString *codeStr = [query substringFromIndex:code.length];
        
        [[WBOAuthViewModel shared] getAccess_token:codeStr completion:^(BOOL isSuccess) {
            
            if (isSuccess) {
                
//                [UIApplication sharedApplication].keyWindow.rootViewController = [[WBWelcomeController alloc]init];
                [[NSNotificationCenter defaultCenter] postNotificationName:kChangeRootVc object:NSStringFromClass([WBWelcomeController class])];
                
            }else{
            
               [SVProgressHUD showErrorWithStatus:@"网络有问题哦，请重试"];
            
            }
        }];
        return NO;
        
    }
    
    return YES;
}
    


@end



